package cc.admin.modules.graduate.entity.sys;

public class Tree {
    private TreeNode root;

    public Tree(String rootValue) {
        this.root = new TreeNode(rootValue);
    }

    public TreeNode getRoot() {
        return root;
    }

    public void addNode(String parentValue, String childValue) {
        TreeNode parentNode = findNode(root, parentValue);
        if (parentNode != null) {
            parentNode.addChild(new TreeNode(childValue));
        } else {
            System.out.println("Parent node not found: " + parentValue);
        }
    }

    public TreeNode findNode(TreeNode currentNode, String value) {
        if (currentNode.getValue().equals(value)) {
            return currentNode;
        }
        for (TreeNode child : currentNode.getChildren()) {
            TreeNode foundNode = findNode(child, value);
            if (foundNode != null) {
                return foundNode;
            }
        }
        return null;
    }

    public void editNode(String oldValue, String newValue) {
        TreeNode nodeToEdit = findNode(root, oldValue);
        if (nodeToEdit != null) {
            nodeToEdit.setValue(newValue);
        } else {
            System.out.println("Node not found: " + oldValue);
        }
    }

    @Override
    public String toString() {
        return "Tree{" +
                "root=" + root +
                '}';
    }
}
